﻿using Entity;
using ServiceCore;
using ServiceCore.Tool.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Customer.Bussiness_Service
{
    public interface Imch_type_service
    {
        ResultObjModel get_mch_type();
        ResultObjModel save_mch_type(List<mch_type> list);
        ResultObjModel get_kid_mch_type();
    }
    public class mch_type_service: Imch_type_service
    {
        private readonly IToole_DI _tool;

        private readonly IMchType_Helper _mchtype;

        public mch_type_service(IToole_DI Tool, IMchType_Helper Mchtype)
        {
            _tool = Tool;
            _mchtype = Mchtype;
        }

        /// <summary>
        /// 获取机器类型
        /// </summary>
        /// <returns></returns>
        public ResultObjModel get_mch_type()
        {
            return CoreStartDo.ResultAction(result =>
            {
                result.code = 0;
                result.msg = "success";
                var temp_list = _mchtype.GetStructure_Template();
                var resultdate = from o in temp_list
                                 select new
                                 {
                                     label = o.type_name,
                                     id = o.type_id,
                                     children = from c in o.kid_type_name
                                                select new
                                                {
                                                    label = c
                                                }
                                 };
                //result.data = _template.GetStructure_Template();
                result.data = resultdate;
                return result;
            });
        }
        /// <summary>
        /// 获取机器二级类型
        /// </summary>
        public ResultObjModel get_kid_mch_type()
        {
            return CoreStartDo.ResultAction(result =>
            {
                var temp_list = _mchtype.GetStructure_Template();
                List<object> resultlist = new List<object>();
                foreach (var item in temp_list)
                {
                    foreach (var item2 in item.kid_type_name)
                    {
                        resultlist.Add(new { id = $"{(int)item.type_id}_{item.type_name}_{item2}", value = item2 });
                    }
                }
                result.code = 0;
                result.msg = "success";
                result.data = resultlist;
                return result;
            });
        }
        /// <summary>
        /// 保存机器类型
        /// </summary>
        /// <returns></returns>
        public ResultObjModel save_mch_type(List<mch_type> list)
        {
            return CoreStartDo.ResultAction(result =>
            {
                _mchtype.WriteTemplate(list);
                result.code = 0;
                result.msg = "success";
                return result;
            });
        }
    }  
}
